package com.changgou.search.controller;

import com.changgou.entity.Page;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.Set;

/**
 * @author kfzhao
 * @version 1.0
 * @className SearchController
 * @description
 * @date 2021/12/28 1:36
 */
//@RestController
@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping("/list")
    public String list(@RequestParam Map<String, String> searchMap, Model model) {
        // 特殊符号处理
        this.handleSearchMap(searchMap);
        // 获取查询结果
        Map resultMap = searchService.search(searchMap);
        model.addAttribute("result", resultMap);
        model.addAttribute("searchMap", searchMap);

        // 封装分页数据并返回
        // 总记录数 当前页 每页显示的条数
        Page<SkuInfo> page = new Page<>(
                Long.parseLong(String.valueOf(resultMap.get("total"))),
                Integer.parseInt(String.valueOf(resultMap.get("pageNum"))),
                Page.pageSize);

        model.addAttribute("page", page);

        // 拼装url
        StringBuilder url = new StringBuilder("/search/list");
        if (!CollectionUtils.isEmpty(searchMap)) {
            // 有查询条件
            url.append("?");
            for (String paramKey : searchMap.keySet()) {
                if (!"sortRule".equalsIgnoreCase(paramKey) && !"sortField".equalsIgnoreCase(paramKey)
                        && !"pageNum".equalsIgnoreCase(paramKey)) {
                    url.append(paramKey).append("=").append(searchMap.get(paramKey)).append("&");
                }
            }
            String urlString = url.toString();
            // 去除最后一个&
            urlString = urlString.substring(0, urlString.length() - 1);
            model.addAttribute("url", urlString);
        } else {
            // 当前没有查询条件
            model.addAttribute("url", url);
        }

        return "search";
    }


    @ResponseBody
    @GetMapping
    public Map search(@RequestParam Map<String, String> searchMap) {
        // 对get请求的参数的编解码的特殊符号做处理
        handleSearchMap(searchMap);
        Map searchResult = searchService.search(searchMap);
        return searchResult;
    }

    /**
     * 对get请求的路径上的参数做处理
     * 因为涉及到编解码操作，所以对key做处理，防止spec 规格查询时出问题
     *
     * @param searchMap
     * @return void
     * @throws
     * @description
     * @author kfzhao
     * @date 2021/12/28 1:41
     */
    private void handleSearchMap(Map<String, String> searchMap) {
        Set<Map.Entry<String, String>> entries = searchMap.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            if (entry.getKey().startsWith("spec_")) {
                searchMap.put(entry.getKey(), entry.getValue().replace("+", "%2B"));
            }
        }
    }
}
